package cn.school.controller.workflow;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.exception.ApiException;
import cn.school.workflow.service.UsePersonApplyService;
import cn.school.workflow.vo.UsePersonApplyVO;
import cn.school.workflow.vo.WorkFlowVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/usePersonApply")
@Api(tags = "部门用人流程接口")
public class UsePersonApplyController {

    @Autowired
    private UsePersonApplyService usePersonApplyService;

    /**
     * 分页查询部门用人申请单
     * @return
     * @throws ApiException
     */
    @PostMapping("/pageList")
    @ApiOperation("list => 分页查询部门用人申请单")
    public CommonResult<CommonPage<UsePersonApplyVO>> findUsePersonApplyPageList(@RequestBody UsePersonApplyVO vo) throws ApiException{
        IPage<UsePersonApplyVO> pageList =  usePersonApplyService.findUsePersonApplyPageList(vo);
        return  CommonResult.success(CommonPage.restPage(pageList));
    }

    /**
     * 创建部门用人申请单
     * @return
     * @throws ApiException
     */
    @PostMapping("/add")
    @ApiOperation("list => 创建部门用人申请单")
    public CommonResult<CommonPage<UsePersonApplyVO>> addUsePersonApply(@RequestBody UsePersonApplyVO vo) throws ApiException{
        int num =  usePersonApplyService.addUsePersonApply(vo);
        if(num==-1){
            return CommonResult.failed("找不到流程审批人，提交失败");
        }
        if(num<1){
            return CommonResult.failed();
        }
        return  CommonResult.success();
    }

    /**
     * 创建部门用人申请单
     * @return
     * @throws ApiException
     */
    @PostMapping("/cancel")
    @ApiOperation("cancel => 撤销部门用人申请单")
    public CommonResult<CommonPage<UsePersonApplyVO>> cancelUsePersonApply(@RequestBody WorkFlowVO vo) throws ApiException{
        boolean flag =  usePersonApplyService.cancelUsePersonApply(vo);
        if(flag){
            return CommonResult.success();
        }
        return  CommonResult.failed("流程取消失败");
    }

    /**
     * 创建部门用人申请单
     * @return
     * @throws ApiException
     */
    @GetMapping("/detail/{processInstanceId}")
    @ApiOperation("detail => 撤销部门用人申请单")
    public CommonResult<UsePersonApplyVO> findUsePersonApplyDetail(@PathVariable("processInstanceId") Long processInstanceId) throws ApiException{
        UsePersonApplyVO usePersonApplyVO =  usePersonApplyService.findUsePersonApplyDetail(processInstanceId);
        if(usePersonApplyVO==null){
            return  CommonResult.failed("查询不单申请单");
        }
        return  CommonResult.success(usePersonApplyVO);
    }
}
